﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

public partial class TruckManage_TruckEdit : BasePage
{
    IBNTruckHandler truckManager = new BNTruckHandler();
    IBNDriverHandler driverManager = new BNDriverHandler();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                ISystemHandler sysManage = new SystemHandler();
                string strPermission = sysManage.GetLoginUserPermissions();
                if (!(strPermission.Contains("1039") || strPermission.Contains("1040")))
                {
                    Response.Redirect("~/NotPurview.aspx");
                    return;
                }
            }

            FillType();
            dataBind();
        }
    }
    private void dataBind()
    {
        if (Request["PKID"] != null)
        {
            BN_TruckQuery query = truckManager.GetTruckByID(int.Parse(Request["PKID"]));
            BN_DriverQuery driverQuery = driverManager.GetDriverByID(query.Driver);
            if (query != null)
            {
                txtLicenseNO.Text = query.LicenseNO;
                ddlType.SelectedValue = query.Type.ToString();
                txtCustomsNO.Text = query.CustomsNO;
                txtICNO.Text = query.ICNO;
                txtCustomsPWD.Text = query.CustomsPWD;
                if (query.YCDate != System.DateTime.MinValue)
                {
                    txtYCDate.Text = query.YCDate.ToString("yyyy-MM-dd");
                }
                if (query.BIDate != System.DateTime.MinValue)
                {
                    txtBIDate.Text = query.BIDate.ToString("yyyy-MM-dd");
                }
                if (query.Status == 0)
                {
                    rad0.Checked = true;
                    rad1.Checked = false;
                    rad2.Checked = false;
                }
                else if (query.Status == 1)
                {
                    rad0.Checked = false;
                    rad1.Checked = true;
                    rad2.Checked = false;
                }
                else if (query.Status == 2)
                {
                    rad0.Checked = false;
                    rad1.Checked = false;
                    rad2.Checked = true;
                }
                txtRemark.Text = query.Remark;
            }
            // 司机
            if (driverQuery != null)
            {
                txtDriver.Text = driverQuery.Name;
                if (driverQuery.Sex == 0)
                {
                    rbSex0.Checked = true;
                    rbSex1.Checked = false;
                }
                else
                {
                    rbSex0.Checked = false;
                    rbSex1.Checked = true;
                }
                txtIDCard.Text = driverQuery.IDCard;
                txtAddress.Text = driverQuery.Address;
                txtMPhone.Text = driverQuery.MPhone;
                txtLicence.Text = driverQuery.Licence;
                txtBail.Text = driverQuery.Bail.ToString();

            }
        }
    }

    private void FillType()
    {
        ISyContainerModelHandler cmManager = new SyContainerModelHandler();
        DataTable dt = cmManager.GetModelList("select * from Sy_ContainerModel where status = 1", new SqlParameter[] { });
        ddlType.DataSource = dt;
        ddlType.DataTextField = "Model";
        ddlType.DataValueField = "PKID";
        ddlType.DataBind();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        BN_TruckQuery truckQuery = new BN_TruckQuery();

        truckQuery.ClientID = int.Parse(Session["LoginUserClientID"].ToString());
        truckQuery.Type = int.Parse(ddlType.SelectedValue);
        truckQuery.LicenseNO = txtLicenseNO.Text;
        truckQuery.CustomsNO = txtCustomsNO.Text;
        truckQuery.ICNO = txtICNO.Text;
        truckQuery.CustomsPWD = txtCustomsPWD.Text;
        if (txtYCDate.Text != null && txtYCDate.Text.Trim() != "")
        {
            truckQuery.YCDate = DateTime.Parse(txtYCDate.Text);
        }
        if (txtBIDate.Text != null && txtBIDate.Text.Trim() != "")
        {
            truckQuery.BIDate = DateTime.Parse(txtBIDate.Text);
        }
        
        truckQuery.Status = (rad0.Checked ? 0 : (rad1.Checked ? 1 : 2));
        truckQuery.Remark = txtRemark.Text;

        truckQuery.LastModUser = int.Parse(Session["UserID"].ToString());
        truckQuery.LastModDate = DateTime.Now;

        BN_DriverQuery driverQuery = new BN_DriverQuery();
        driverQuery.ClientID = truckQuery.ClientID;
        driverQuery.Name = txtDriver.Text;
        if (rbSex0.Checked)
        {
            driverQuery.Sex = 0;
        }
        else
        {
            driverQuery.Sex = 1;
        }
        driverQuery.IDCard = txtIDCard.Text;
        driverQuery.Address = txtAddress.Text;
        driverQuery.MPhone = txtMPhone.Text;
        driverQuery.Licence = txtLicence.Text;
        if (txtBail.Text != null && !txtBail.Text.Trim().Equals(""))
        {
            driverQuery.Bail = int.Parse(txtBail.Text.Trim());
        }
        driverQuery.Status = 1;

        try
        {
            if (Request["PKID"] != null)
            {
                BN_TruckQuery tmp = truckManager.GetTruckByID(int.Parse(Request["PKID"]));
                if (tmp == null)
                {
                    Jscript.Alert(Resources.Messages.RecordNotFound, this.Page);
                }
                else
                {
                    truckQuery.PKID = tmp.PKID;
                    truckQuery.IsFree = tmp.IsFree;

                    int driverID = 0;
                    if (truckQuery.Driver != null && truckQuery.Driver != 0)
                    {
                        driverQuery.PKID = truckQuery.Driver;
                        driverManager.UpdateDriver(driverQuery);
                        driverID = driverQuery.PKID;
                    }
                    else
                    {
                        driverManager.AddDriver(driverQuery, ref driverID);
                    }
                    truckQuery.Driver = driverID;
                    if(truckManager.UpdateTruck(truckQuery))
                    {
                        Jscript.AlertAndRedirect(Resources.Messages.UpdateSuccess , "TruckList.aspx", this.Page);
                    }
                    else
                    {
                        Jscript.Alert(Resources.Messages.UpdateFailed, this.Page);
                    }
                }
            }
            else
            {
                truckQuery.IsFree = 1;
                int driverID = 0;
                driverManager.AddDriver(driverQuery, ref driverID);
                truckQuery.Driver = driverID;
                if (truckManager.AddTruck(truckQuery))
                    {
                        Jscript.AlertAndRedirect(Resources.Messages.AddSuccess, "TruckList.aspx", this.Page);
                    }
                    else
                    {
                        Jscript.Alert(Resources.Messages.AddFailed, this.Page);
                    }
            }
        }
        catch
        {
            Jscript.Alert(Resources.Messages.SaveFailed, this.Page);
        }
        
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("TruckList.aspx");
    }
}
